﻿using Setme.Domain.Model;
using Setme.Domain.Repository;
using System.Linq;

namespace Setme.Domain.Infrastructure.Impl
{
    /// <summary>
    /// EntityFramework仓储基类
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class Repository<TEntity> : IRepository<TEntity> where TEntity : class, IAggregateRoot
    {
        public Repository(IRepositoryContext _repositoryContext)
        {
            repositoryContext = _repositoryContext;
        }

        private readonly IRepositoryContext repositoryContext;

        //private Setme.Domain.Repository.Impl.EntityFrameworkRepositoryContext repositoryContext
        //{
        //    get
        //    {
        //        if (repositoryContext is Setme.Domain.Repository.Impl.EntityFrameworkRepositoryContext)
        //        {
        //            return repositoryContext as Setme.Domain.Repository.Impl.EntityFrameworkRepositoryContext;
        //        }
        //        throw new System.Exception(string.Format("数据仓储上下文对象类型不正确，应为EntityFrameworkUnitOfWork，实际为 {0}", repositoryContext.GetType().Name));
        //    }
        //    set;
        //}

        public void Insert(IAggregateRoot aggre)
        {
            repositoryContext.RegisterCreate(aggre);
        }

        public void Delete(IAggregateRoot aggre)
        {
            repositoryContext.RegisterDeleted(aggre);
        }

        public void Update(IAggregateRoot aggre)
        {
            repositoryContext.RegisterUpdate(aggre);
        }

        public IQueryable<TEntity> GetEntity
        {
            get { return repositoryContext.GetEntity<TEntity>(); }
        }
    }
}
